home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 5
/
Apprentice-Release5.iso
/
Source Code
/
Libraries
/
Sherlock 2.0
/
SDEL
/
SDELsrc
/
sdel_arg.c
next >
Wrap
Text File
|
1996-04-02
|
5KB
|
267 lines
/*
SDEL: argument processing.
source: sdel_arg.c
started: April 2, 1996.
version: April 2, 1996.
*/
#include "sdel.h"
#include "sdel_arg.h"
#include <LIBend.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
// Public globals
bool disable_flag = FALSE; /* TRUE: retain SL_DISABLE */
bool include_flag = FALSE; /* TRUE: remove #include sl.h */
bool name_flag = FALSE; /* TRUE: retain SL_NAME */
bool nest_flag = FALSE; /* TRUE: allow nested comments. */
bool trigraph_flag = FALSE; /* TRUE: output trigraphs. */
// Private prototypes
static void help (void);
static void set_syn (char * id1, char * id2);
static char * str_alloc (char * string);
static void synonyms (void);
/*
Handle command line arguments.
*/
void
arg_do_argv(int argc, char **argv)
{
char * arg = NULL;
SL_DISABLE();
/* Test for correct command line. */
if (argc < 3) {
help();
end_usage();
}
/* Process all the arguments on the command line. */
argc--;
argv++;
while (argc-- > 0) {
arg = *argv++;
if (str_eq(arg, "-d")) {
disable_flag = TRUE;
}
else if (str_eq(arg, "-f")) {
if (argc--) {
arg = *argv++;
syn_file_name = arg;
syn_file = fopen(syn_file_name, "r");
if (in_file) {
es("Can not open synonym file: "); es(syn_file_name); enl();
end_quit();
}
synonyms();
fclose(syn_file);
syn_file = NULL;
}
else {
es("Trailing -f");
end_usage();
}
}
else if (str_eq(arg, "-i")) {
/* Remove #include "sl.h" or <sl.h> */
include_flag = TRUE;
}
else if (str_eq(arg, "-n")) {
/* Disallow nested comments. */
nest_flag = FALSE;
}
else if (str_eq(arg, "-t")) {
/* Allow trigraph translation. */
trigraph_flag = TRUE;
}
else if (str_eq(arg, "-r")) {
/* Retain SL_NAME. */
name_flag = TRUE;
}
else if (str_eq(arg, "-?")) {
/* Ignore it. */
;
}
else if (in_file_name == NULL) {
in_file_name = arg;
}
else if (out_file_name == NULL) {
out_file_name = arg;
}
else {
es("Extra file argument: "); es(arg);
end_usage();
}
}
/* Make sure that both file arguments were provided. */
if (in_file_name == NULL) {
es("Missing input, output file arguments\n");
end_usage();
}
else if (out_file_name == NULL) {
es("Missing output file argument\n");
end_usage();
}
else if (str_eq(in_file_name, out_file_name)) {
es("Can not copy input file to output file\n");
end_usage();
}
/* Open the files. */
#if 0
out_file = fopen(out_file_name, "r");
if (out_file) {
es("Output file: "); es(out_file_name); es(" exists"); enl();
end_usage();
}
#endif
out_file = fopen(out_file_name, "w");
if (!out_file) {
es("Can not create "); es(out_file_name); enl();
end_usage();
}
in_file = fopen(in_file_name, "r");
if (!in_file) {
es("Can not open "); es(in_file_name); enl();
end_usage();
}
}
static void
help (void)
{
es("SDEL in out [options]\n\n");
es("-d retain SL_DISABLE macros\n");
es("-f <file> rename macros using a synonym file\n");
es("-i remove #include sl.h lines\n");
es("-n disallow nested comments\n");
es("-r retain SL_NAME macros\n");
es("-t allow trigraph translation\n");
es("-? print version number and exit\n");
}
/*
Change the spelling of a Sherlock macro to a synonym.
*/
static void
set_syn(char * id1, char * id2)
{
int i;
/* Special cases. */
if (str_eq(id1, r_void)) {
r_void = str_alloc(id2);
return;
}
else if (str_eq(id1, s_disable)) {
s_disable = str_alloc(id2);
return;
}
else if (str_eq(id1, s_name)) {
s_name = str_alloc(id2);
return;
}
/* All return macros except RETURN_VOID. */
for (i = 0; rmn_tab[i] != NULL; i++) {
if (str_eq(rmn_tab[i], id1)) {
rmn_tab[i] = str_alloc(id2);
return;
}
}
/* All other Sherlock macros. */
for (i = 0; mn_tab[i] != NULL; i++) {
if (str_eq(mn_tab[i], id1)) {
mn_tab[i] = str_alloc(id2);
return;
}
}
es("Synonym entry for "); es(id1); es(" has no effect.\n");
}
/*
Allocate memory big enough to hold the string,
then copy the string to the allocated memory.
*/
char *
str_alloc(char *s)
{
char * p;
int n;
n = strlen(s) + 1;
p = malloc(n);
strcpy(p, s);
return p;
}
/*
Process a synonym file.
*/
void
synonyms(void)
{
char buffer1 [1000];
char buffer2 [1000];
skip_bl();
while (ch != EOF) {
if (isid1(ch)) {
get_id(&buffer1[0]);
skip_bl();
if (isid1(ch))
{
get_id(&buffer2[0]);
set_syn(&buffer1[0], &buffer2[0]);
skip_bl();
if (ch == '\n') {
next_ch();
line++;
skip_bl();
}
else {
error(es("Newline expected in synonym file"));
end_usage();
}
}
else {
error(es("Synonym expected in synonym file"));
end_usage();
}
}
else if (ch == ' ' || ch == '\t' || ch == '\n') {
if (ch == '\n') {
line++;
}
next_ch();
}
else if (ch == '#') {
while (ch != '\n' && ch != EOF) {
next_ch();
}
}
else {
error(es("Identifier expected in synonym file"));
end_usage();
}
}
}